/********************************************************************
 * PROJECT: CSAIR ROST PROJECT.
 * SUPPLIER: FOREVER OPEN SOURCE SOFTWARE INC.
 ********************************************************************
 * FILE: GanttTask.java
 * (C) Copyright Foliage Team 2013, All Rights Reserved.
 ********************************************************************/
package org.foliage.rcp.ui.widgets;

import java.util.Calendar;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Item;
import org.foliage.rcp.ui.preference.GanttTaskSkin;

@SuppressWarnings("rawtypes")
public class GanttTask extends Item implements Comparable {

	// ----------------------------------------------- 

	private GanttItem item;

	private Calendar lowerCalendar;

	private Calendar upperCalendar;

	private Rectangle bounds;

	private GanttTaskSkin skin;

	// ----------------------------------------------- 

	public GanttTask(GanttItem item) {
		this(item, null, null);
	}

	public GanttTask(GanttItem item, Calendar lowerLimit, Calendar upperLimit) {
		super(item, SWT.NONE);
		this.item = item;
		this.lowerCalendar = lowerLimit;
		this.upperCalendar = upperLimit;
		this.item.newTask(this, -1);
	}

	// ----------------------------------------------- 

	@Override
	public int compareTo(Object obj) {
		GanttTask task = (GanttTask) obj;
		return getLowerCalendar().compareTo(task.getLowerCalendar());
	}

	// ----------------------------------------------- 

	public boolean isIntersect(Calendar lowerLimit, Calendar upperLimit) {
		long localInterval = upperCalendar.getTimeInMillis() - lowerCalendar.getTimeInMillis();
		long paramInterval = upperLimit.getTimeInMillis() - lowerLimit.getTimeInMillis();
		long sumInterval = Math.max(upperLimit.getTimeInMillis() - lowerCalendar.getTimeInMillis(), upperCalendar.getTimeInMillis() - lowerLimit.getTimeInMillis());
		return sumInterval < localInterval + paramInterval;
	}

	// ----------------------------------------------- 

	public Rectangle getBounds() {
		return bounds;
	}

	public void setBounds(Rectangle bounds) {
		this.bounds = bounds;
	}

	public Calendar getLowerCalendar() {
		return lowerCalendar;
	}

	public void setLowerCalendar(Calendar lowerCalendar) {
		this.lowerCalendar = lowerCalendar;
	}

	public Calendar getUpperCalendar() {
		return upperCalendar;
	}

	public void setUpperCalendar(Calendar upperCalendar) {
		this.upperCalendar = upperCalendar;
	}

	public GanttTaskSkin getSkin() {
		return skin;
	}

	public void setSkin(GanttTaskSkin skin) {
		this.skin = skin;
	}

	// -----------------------------------------------

	public GanttItem getItem() {
		return item;
	}

}
